package com.yuma.order.service.impl;

import com.yuma.order.entity.Order;
import com.yuma.order.feign.AccountFeignClient;
import com.yuma.order.feign.StorageFeignClient;
import com.yuma.order.mapper.OrderMapper;
import com.yuma.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private StorageFeignClient storageFeignClient;
    @Autowired
    private AccountFeignClient accountFeignClient;


//    @Transactional
    @GlobalTransactional   // 事务发起方，其他节点事务都还是使用@Transactional
    @Override
    public int create(Order order) {
        // 1.扣减库存
        storageFeignClient.deduct(order.getProductId(), order.getCount());
        // 2.扣减余额
        accountFeignClient.debit(order.getUserId(), order.getMoney());

        //int i = 1/0;
        // 3.创建订单
        return orderMapper.insert(order);
    }
}
